<template>
	<view>
		<uni-nav-bar ref="navBar" fixed statusBar title="分销设置" :border="false" backgroundColor="#ffffff" @clickLeft="goBack">
			<template v-slot:left>
				<image class="size-32" src="@/static/svg/icon-back.svg" />
			</template>
		</uni-nav-bar>
		<view class="m-12">
			<view v-for="(item, index) in data" :key="index" class="mt-12 radius-8 bg-white p-12">
				<view class="h-20 mb-8 flex justify-between text-color-regular fs-14 font-normal">
					<text class="title">{{ item.title }}</text>
					<view v-if="item.configType === 'BOOLEAN'" class="flex item-center">
						<image v-if="item.configValue === 'true'" class="w-32 h-16" src="@/static/svg/switch-on.svg" @click="onSwitchClick(item.configKey, 'false')"></image>
						<image v-else class="w-32 h-16" src="@/static/svg/switch-default.svg" @click="onSwitchClick(item.configKey, 'true')"></image>
					</view>
					<view v-else class="flex item-center" @click="openCommission(item.configKey, item.configValue)">
						<text>{{ item.configValue ? item.configValue + '%' : '未设置' }}</text>
						<image class="size-20" src="@/static/svg/icon-arrow-right.svg"></image>
					</view>
				</view>
				<view class="tip">{{ item.description }}</view>
			</view>
		</view>
		<setCommission ref="commissionRef" @refresh="getList" />
	</view>
</template>

<script setup>
	import {
		ref,
		watch,
		getCurrentInstance
	} from 'vue'
	import {
		mathHeight
	} from '@/utils/index.js'
	import { list, saveConfig } from '@/pagesH/api/subscribe.js'
	import {onLoad,onShow} from '@dcloudio/uni-app'
	import setCommission from './components/setCommission';
	const h = ref(0)
	h.value = mathHeight(120)
	const data = ref([])
	const commissionRef = ref()
	
	const openCommission = (configKey, val) => {
		commissionRef.value.showPop(true, configKey, val)
	}
	const getList = async () => {
		uni.showLoading()
		data.value = await list()
		uni.hideLoading()
	}
	const goBack = () => {
		if(getCurrentPages().length === 1) {
			uni.reLaunch({
				url: '/pages/home/home'
			})
			return
		}
		uni.navigateBack()
	}
	const onSwitchClick = (configKey, configValue) => {
		uni.showLoading()
		saveConfig({configKey, configValue}).then(async res => {
			uni.hideLoading()
			uni.showToast({
				icon: 'none', 
				title: '设置成功'
			})
			getList()
		}).catch(err => uni.hideLoading())
	}
	onShow(() => {
		getList()
	})
</script>

<style scoped lang="scss">
	.tip{
		font-size: 10px;
		color: rgba(51,51,51,0.6);
	}
	.title{
		font-weight: 600;
		font-size: 14px;
		color: #333333;
	}
</style>